﻿var ROOT_API = 'http://matchtest.winwayworld.com/match/';

var ROUTE_URL = 'http://ucentertest.winwayworld.com/match/common/wxRoute/';

//生成二维码链接
var QRcode = 'http://matchtest.winwayworld.com/wxQrcode/route';

var PKTZ_API = {


    /** 公用方法 **/
		
	//二维码生成
    createQRcode: function (param) {
        $('#qrCode').qrcode({
            width: 230,
            height: 230,
            text: QRcode + "?teamId=" + param.teamId
        });
    },
    
    //获取cookies
    getCookie: function (cookieName) {
        var strCookie = decodeURI(document.cookie);
        var arrCookie = strCookie.split("; ");
        for (var i = 0; i < arrCookie.length; i++) {
            var arr = arrCookie[i].split("=");
            if (cookieName == arr[0]) {
                return arr[1];
            }
        }
        return "";
    },

    //页面路由
    getPageRoute: function (url) {
        window.location.href = ROUTE_URL + url;
    },

    //获取所有队伍的总积分
    getAllTeamScore: function () {
        $.ajax({
            url: ROOT_API + 'team/allTeamScore',
            type: 'GET',
            dataType: 'JSON',
            success: function (response) {
                var code = response.code;
                var data = response.data;
                if(code == 0){
                    $('.all-score').text(data);
                }
            }
        })
    },

    /** 用户登录 **/
    //账号密码登录
    checkPasswordLoin: function (param) {
        $.ajax({
            url: ROOT_API + 'wxlogin/check/password',
            type: 'GET',
            data: param,
            dataType: 'JSON',
            contentType: "application/x-www-form-urlencoded",
            success: function (response) {
                var code = response.code;
                var data = response.data;
                if(code == 0){
                    weui.toast('登录成功', {
                        duration: 1000,
                        className: 'custom-classname',
                        callback: function(){
                            PKTZ_API.getPageRoute('index');
                        }
                    });
                }else {
                    $.mvalidateTip(response.msg)
                }
            }
        })
    },
    //通过短信验证码登录
    checkVerifyCodeLogin: function (param) {
        $.ajax({
            url: ROOT_API + 'wxlogin/check/verifyCode',
            type: 'GET',
            data: param,
            dataType: 'JSON',
            contentType: "application/x-www-form-urlencoded",
            success: function (response) {
                var code = response.code;
                if(code == 0){
                    weui.toast('登录成功', {
                        duration: 1000,
                        className: 'custom-classname',
                        callback: function(){
                            PKTZ_API.getPageRoute('index');
                        }
                    });
                }else $.mvalidateTip(response.msg);
            }
        })
    },

    /** 个人中心 **/
    //获取个人信息
    getMemberInfo: function (param,callback) {
        $.ajax({
            url: ROOT_API + 'member/memberInfo',
            type: 'GET',
            data: param,
            dataType: 'JSON',
            success: function (response) {
                var code = response.code;
                var data = response.data;
                if(code == 0){
                    var teamId = data.teamId;
                    var userType = data.userType;
                    $('.mobile-number').text(data.mobile);
                    $('.hospital').text(data.hospitalName);
                    $('.personType').text(data.userTypeName).data('userType', data.userType);
                    $('.personal-information').find('.score').text(data.score);
                    $('.personPosition').text(data.position);
                    $('#head img').attr('src', data.head || 'images/apply/defaultIcon.png');
                    $('#personHead').data('personId', data.id);
                    $('#personName').text(data.name);

                    if(userType != 2){
                        $('.team-information .captain').css('pointer-events', 'none')
                    }else {
                        $('.team-information .captain').css('pointer-events', 'auto')
                    }
                }
            }
        })
    },
    //通过手机号获取队伍信息
    getTeamInfoByMobile: function (param,callback) {
        $.ajax({
            url: ROOT_API + 'team/getTeamInfo',
            type: 'GET',
            data: param,
            dataType: 'JSON',
            success: function (response) {
                var code = response.code;
                var teamData = response.data;
                if(code == 0){
                    var len = teamData.length;
                    if(len > 1){
                        $('.team-information').find('.team-tab').show();
                        $('.team-information .team-tab_content').find('.item').each(function (t) {
                            $(this).text(teamData[t].teamName);
                        });
                    }
                    for(var i = 0; i < len; i++){
                        var $this =  $('.team-information .tab-center-ul').eq(i);
                        $this.find('.teamName').text(teamData[i].teamName).data('teamId',teamData[i].id);
                        $this.find('.teamImg').attr('src', teamData[i].img);
                        $this.find('.score').text(teamData[i].score);
                        $this.find('.slogan').text(teamData[i].slogan);
                        $this.find('.number').text(teamData[i].examScore);
                        $this.find('.challengeTeam').text(teamData[i].challengeTeam);

                        //队员
                        var members = teamData[i].teamMember;
                        var arr = [];
                        for(var j = 0; j < members.length; j++){
                            arr[j] = members[j].name;
                        }
                        $this.find('.teamMember').text(arr);

                        //资格卡
                        var cardList = teamData[i].cardList;
                        for(var c = 0; c < cardList.length; c++){
                            var pic = '<img src="'+ cardList[c] +'">';
                            $this.find('.card').append(pic);
                        }

                        //邀请队员
                        var group = teamData[i].groupId;
                        if((group == 'A组' && members.length < 5) || (group == 'B组' && members.length < 3)){
                            $this.parent('.team-content').find('.beckon').show();
                        }else {
                            $this.parent('.team-content').find('.beckon').hide();
                        }

                        var param = {
                            position: $('.personPosition').text(),
                            groupId: teamData[i].groupId,
                            teamId: teamData[i].id
                        };
                        callback(param)
                    }
                }
            }
        })
    },
    //获取队伍信息
    /*getTeamInfo: function (param, callback) {
        $.ajax({
            url: ROOT_API + 'team/teamInfo',
            type: 'GET',
            data: param,
            dataType: 'JSON',
            success: function (response) {
                var code = response.code;
                var data = response.data;
                if(code == 0){
                    var group = data.groupId;
                    var length = data.teamMember.length;
                    $('.team-information').find('.hospitalName').text(data.hospitalName).data('matchArea', data.matchArea);
                    $('.team-information').find('.teamName').text(data.teamName).data('teamId', data.id);
                    $('.team-information').find('.challengeTeam').text(data.challengeTeam);
                    var members = data.teamMember;
                    var arr = [];
                    for(var i=0; i < members.length; i++){
                    	arr[i] = members[i].name;
                    }
                    $('.team-information').find('.teamMember').text(arr.join('、'));
                    $('.team-information').find('.slogan').text(data.slogan);
                    $('.team-information').find('.score').text(data.score);
                    $('.team-information').find('.team-pic img').attr('src', data.img || 'images/apply/defaultIcon.png');

                    if(data.isFull == 0){
                        $('.beckon').show();
                    }
                    var param = {
                        position: $('.team-information .position').text(),
                        groupId: data.groupId,
                        teamId: data.id
                    };
                    callback(param);
                }
            }
        })
    },*/

    //修改参赛身份
    updateUserType: function (param) {
        $.ajax({
            url: ROOT_API + 'member/updateUserType',
            type: 'POST',
            data: param,
            dataType: 'JSON',
            contentType: "application/x-www-form-urlencoded",
            success: function(response){
                var code = response.code;
                if(code == 0){
                    weui.toast('修改成功！', {
                        duration: 1000,
                        className: 'custom-classname',
                        callback: function(){
                            var roles = {
                                position: $('.team-information .position').text(),
                                matchArea: $('.team-information').find('.hospitalName').data('matchArea'),
                                teamId: $('.team-information').find('.teamName').data('teamId')
                            };
                            PKTZ_API.getOptionalRoles(roles);
                            var userType = param.userType;
                            if(userType != 2){
                                $('#slogan, #uploadPic').css('pointer-events', 'none')
                            }else {
                                $('#slogan, #uploadPic').css('pointer-events', 'auto')
                            }
                        }
                    });
                }
            }
        })
    },
    //修改队伍口号
    updateTeamInformation: function (param) {
        $.ajax({
            url: ROOT_API + 'team/update',
            type: 'POST',
            data: param,
            dataType: 'JSON',
            contentType: "application/x-www-form-urlencoded",
            success: function(response){
                var code = response.code;
                if(code == 0){
                    weui.toast('修改成功！', {
                        duration: 1000,
                        className: 'custom-classname',
                        callback: function(){
                            // var param = {
                            //     position: $('.team-information .position').text(),
                            //     matchArea: $('.team-information').find('.hospitalName').data('matchArea'),
                            //     teamId: $('.team-information').find('.teamName').data('teamId')
                            // };
                        }
                    });
                }
            }
        })
    },



    //退出登录
    userLogout: function () {
        $.ajax({
            url: ROOT_API + 'member/logout',
            type: 'GET',
            data: '',
            dataType: 'JSON',
            success: function(response){
                var code = response.code;
                if(code == 0){
                    weui.toast('退出成功！', {
                        duration: 1000,
                        className: 'custom-classname',
                        callback: function(){
                            PKTZ_API.getPageRoute('index');
                        }
                    });
                }
            }
        })
    },

    /** 答题板块 **/

    //创建题目列表
    createQuestionList: function (url, param) {

    },
    //创建题目选项列表
    createQuestionOptions: function (options) {
        var optionsData = options;
        optionsData.forEach(function (item) {

        })
    },

    /** 风采展示、报名板块 **/

    //搜索医院
    searchHospital: function (keyWord) {
        $.ajax({
            url: ROOT_API + 'hospital/keyWords',
            type: 'GET',
            data: keyWord,
            dataType: 'JSON',
            success: function (response) {
                var searchData = response.data;
                var code = response.code;
                if(code == 0 && searchData.length > 0){
                    $('#search-hospital').show();
                    $('#search-hospital').empty();
                    searchData.forEach(function (value) {
                        var model = '<p groupId="'+ value.groupId +'" hospitalId="'+ value.hospitalId +'">'+
                            value.hospitalName +
                            '</p>';
                        $('#search-hospital').append(model);
                    })
                }
            }
        })
    },
    //根据队伍ID获取队伍信息
    getTeamData: function (id) {
        $.ajax({
            url: ROOT_API + 'team/teamInfo',
            type: 'GET',
            data: id,
            dataType: 'JSON',
            success: function (response) {
                var code = response.code;
                var data = response.data;
                if(code == 0){
                    var group = data.groupId;
                    var _group = $('#challengeBtn').data('group');
                    var matchArea = data.matchArea;
                    var teamName = data.teamName;
                    var hospitalName = data.hospitalName;
                    var userType = PKTZ_API.getCookie('memberTeamUserType');
                    if(matchArea){
                        $('#challengeBtn').data('status', 2);
                    }else {
                        if(group != _group){
                            $('#challengeBtn').data('status', 3);
                            return
                        }
                    	//var memberMobile = PKTZ_API.getCookie('memberMobile');
                        //var memberTeamId = PKTZ_API.getCookie('memberTeamId');
                        if(userType != 2){
                            $('#challengeBtn').data('status', 4);
                            return
                        }
                        $('#challengeBtn').data('status', 1);
                        $('#challengeDialog').find('.hospital').text(hospitalName).data('groupId', group);
                        $('#challengeDialog').find('.team').text(teamName);
                        $('#challengeDialog').find('.hospital').data('teamId', data.id);
                        PKTZ_API.getMatchAreaByGroup({groupId:group});
                    }

                    //邀请扫描二维码
                    $('#hospital-name').val(hospitalName).data('groupId', group).data('hospitalId', data.hospitalId);
                    $('#teamName').val(teamName).data('teamID', data.id);
                }
            }
        })
    },

    //根据分组，获取赛区列表
    getMatchAreaByGroup: function (id) {
        $.ajax({
            url: ROOT_API + 'register/getMatchAreaByGroup',
            type: 'GET',
            data: id,
            dataType: 'JSON',
            success: function (response) {
                var groupData = response.data;
                var code = response.code;
                var list = [];
                if(code == 0 && groupData){
                    groupData.forEach(function (item) {
                        var t = {
                            label: item.hospital,
                            value: item.groupId,
                            name: item.name
                        };
                        list.push(t);
                    })
                    $('#zone').data('options', JSON.stringify(list));
                }
            }
        })
    },

    //选择完医院和赛区，确认去报名
    submitBaoming: function (param) {
        $.ajax({
            url: ROOT_API + 'register/betting',
            type: 'GET',
            data: param,
            dataType: 'JSON',
            success: function (response) {
                var code = response.code;
                if(code == 0){
                    $('#challengeDialog').hide();
                    weui.toast('挑战成功！', {
                        duration: 1000,
                        className: 'custom-classname',
                        callback: function(){
                            PKTZ_API.getPageRoute('style_detail');
                        }
                    });
                }else{
                	$.mvalidateTip(response.msg)
                }
            }
        })
    },

    //新增医院，获取医院级别列表
    getHospitalLevel: function () {
        $.ajax({
            url: ROOT_API + 'common/getHospitalLevel',
            type: 'GET',
            data: '',
            dataType: 'JSON',
            success: function (response) {
                var code = response.code;
                var levels = response.data;
                var list = [];
                if(code == 0 && levels){
                    levels.forEach(function (item) {
                        var t = {
                            label: item.hospitalLevel,
                            value: item.groupId
                        };
                        list.push(t);
                    })
                    $('#level').data('options', JSON.stringify(list));
                }
            }
        })
    },

    //报名时，新增医院
    addHospital: function (param) {
        $.ajax({
            url: ROOT_API + 'hospital/add',
            type: 'POST',
            //async: false,
            data: param,
            dataType: 'JSON',
            contentType: "application/x-www-form-urlencoded",
            success: function (response) {
                var code = response.code;
                var data = response.data;
                if(code == 0){
                    var hospitalName = data.hospital.hospitalName;
                    var hospitalId = data.hospital.hospitalId;
                    var groupId = data.hospital.groupId;
                    $('#hospital-name').val(hospitalName);
                    $('#hospital-name').data('hospitalId', hospitalId);
                    $('#hospital-name').data('groupId', groupId);
                    $('.block-choice').fadeOut(function () {
                        $('.block-choice').find('input[type="text"]').val('');
                    });
                }
            }
        })
    },
    //发送手机短信验证码
    getVerifyCode: function (param, callback) {
        $.ajax({
            url: ROOT_API + 'common/getVerifyCode',
            type: 'POST',
            async: false,
            data: param,
            dataType: 'JSON',
            contentType: "application/x-www-form-urlencoded",
            success: function (response) {
                var code = response.code;
                var msg = response.msg;
                if(code == 0){
                    $.mvalidateTip(msg);
                    var type = param.type;
                    var call;
                    if(type == 'login'){
                        call = $('.login-send');
                    }else {
                        call = $('.verify-btn');
                    }
                    callback(call)
                }
                $.mvalidateTip(msg);
            }
        })
    },
    //获取医生职称
    getDoctorPrivilege: function () {
        $.ajax({
            url: ROOT_API + 'common/getDoctorPrivilege',
            type: 'GET',
            data: '',
            dataType: 'JSON',
            success: function (response) {
                var code = response.code;
                //var msg = response.msg
                var list = [];
                var groupData = response.data;
                if(code == 0){
                    groupData.forEach(function (value, index) {
                        var t = {
                            label: value,
                            value: index
                        };
                        list.push(t);
                    })
                    $('#position').data('options', JSON.stringify(list));
                }
            }
        })
    },
    //报名时，获取队伍信息
    getTeamsByHospital: function (param) {
        $.ajax({
            url: ROOT_API + 'team/getTeamsByHospital',
            type: 'GET',
            data: param,
            dataType: 'JSON',
            success: function (response) {
                var code = response.code;
                if(code == 0){
                    var teamData = response.data;
                    var listTeam = teamData.listTeam;
                    if(listTeam.length > 0){
                        $("#teamItem").after($.templates("#teamItem").render(listTeam));
                    }
                    var isAllow = teamData.isAllow;
                    if(isAllow){
                        $('.addTeam').show();
                    }
                }
            }
        })
    },

    //报名时，根据所选职称、赛区和队伍，获取可选身份
    getOptionalRoles: function (param) {
        $.ajax({
            url: ROOT_API + 'register/member/roles',
            type: 'GET',
            data: param,
            dataType: 'JSON',
            success: function (response) {
                var code = response.code;
                if(code == 0){
                    var useTypes = response.data.roleAble;
                    var list = [];
                    useTypes.forEach(function (t) {
                        var l = {
                            label: t.roleName,
                            value: t.userType
                        };
                        list.push(l);
                    });
                    $('#userType').data('options', JSON.stringify(list));
                    $('#identity').data('options', JSON.stringify(list));
                }
            }
        })
    },

    //最终报名提交
    applySubmitChallenge: function (param) {
        $.ajax({
            url: ROOT_API + 'wxregister/submit',
            type: 'POST',
            async: false,
            data: param,
            dataType: 'JSON',
            contentType: "application/x-www-form-urlencoded",
            success: function (response) {
                var code = response.code;
                if(code == 0){
                    var member = response.data.member;
                    var team = response.data.team;
                    var teamMember = team.teamMember;
                    var group = team.groupId;
                    var isFull = team.isFull;
                    weui.toast('报名成功！', {
                        duration: 1000,
                        className: 'custom-classname',
                        callback: function(){
                            $('.apply_success').fadeIn();
                            $('.apply_success').find('.score').text(member.score);
                            $('.apply_success').find('.teamName').text(team.teamName);
                            $('.apply_success').find('.typeName').text(member.userTypeName);
                            $('.apply_success').find('.hospitalName').text(team.hospitalName);

                            var members = [];
                            teamMember.forEach(function (t) {
                                var userType = t.userType;
                                var name = t.name;
                                if(userType == 1){
                                    $('.apply_success').find('.mentor').text(name);
                                }else if (userType == 2) {
                                    $('.apply_success').find('.lead').text(name);
                                }else {
                                    members.push(name);
                                }
                            });
                            $('.apply_success').find('.members').text(members.join('、') || '(大神未到)');

                            var length = teamMember.length;
                            if(group == 'A组'){
                                $('.apply_success').find('.non-arrival').text(5 - length);
                            }else {
                                $('.apply_success').find('.mentors').hide();
                                $('.apply_success').find('.non-arrival').text(2 - length);
                            }
                            if(isFull){
                                $('.success_msg').find('.bottom').hide();
                            }
                            $('#applyForm input').not(':button,:submit,:reset,:hidden').val('');
                        }
                    });
                }else $.mvalidateTip(response.msg)
            }
        })
    },

    //报名截止时间查询
    queryDeadline: function () {
        $.ajax({
            url: ROOT_API + 'register/check',
            type: 'GET',
            data: '',
            dataType: 'JSON',
            success: function (response) {
                var code = response.code;
                var msg = response.msg;
                var data = response.data;
                if(code == 0){
                    if(data){
                        PKTZ_API.getPageRoute('apply');
                    }else $.mvalidateTip('报名时间结束了！')
                }else {
                    $.mvalidateTip(msg)
                }
            }
        })
    },


    /** 挑战擂主 **/
    //获取全部擂主列表
    getMatchAreaList: function (param,callback) {
        $.ajax({
            url: ROOT_API + 'common/getMatchAreaList',
            type: 'GET',
            data: param,
            dataType: 'JSON',
            beforeSend: function () {
                //this.loading.show()
            },
            success: function (response) {
                var code = response.code;
                var listData = response.data;
                if(code == 0){
                   listData.forEach(function (value) {
                       var name = value.name;
                       var hospital = value.hospital;
                       var hot = value.hot;
                       var cover = value.cover
                       var src = value.video
                       var hots = hot.replace(/%/, "");
                       var _hots = ((hots * 85)/100) + '%';
                       //风采展示
                       $('#showList>li').each(function () {
                           var _name = $(this).attr('title-id')
                           if(name == _name){
                               $(this).find('.first-hospital').text(hospital);
                               $(this).find('.left-bar').attr('name', name);
                               $(this).find('.inner-box img').attr('src', cover);
                               $(this).find('.hot-number').text(hot);
                               $(this).find('.ruling').css({
                                   height: _hots
                               })
                           }
                       });
                       //风采展示详情，赛区列表；
                       $('#zone-list .list-ul li').each(function () {
                           var _name = $(this).find('.title').text();
                           if(name == _name){
                               $(this).find('.progress-number').text(hot);
                               $(this).find('.scale').css({
                                   width: hots + '%'
                               })
                           }
                       });

                       //当前赛区详情
                       var current = PKTZ_API.getCookie('matchArea');
                       if(name == current){
                           $('#video').attr('src', src);
                           $('#video').attr('poster', cover);
                           $('.go-challenge').find('.hot').text(hot);
                           $('.go-challenge').find('.zone-name').text(name);
                           $('.go-challenge').find('.hospital-name').text(hospital);
                           $('#challengeBtn').data('group', value.groupId);
                           $('#zone').val(hospital).data('matchArea', name);
                           var isFull = value.isFull;
                           if(!isFull){
                               $('#challengeBtn').data('isFull', false);
                               $('#challengeBtn').data('status', 0);
                           }else {
                               $('#challengeBtn').data('isFull', true).addClass('grays');
                           }
                           var teamId = PKTZ_API.getCookie('memberTeamId');
                           if(teamId){
                               callback({teamId:teamId})
                           }
                       }
                   });

                }
            }
        })
    },
    //根据赛区名获取队伍信息
    getTeamList: function (param) {
        $.ajax({
            url: ROOT_API + 'team/getTeamList',
            type: 'GET',
            data: param,
            dataType: 'JSON',
            success: function (response) {
                var code = response.code;
                var data = response.data.list;
                if(code == 0){
                    $("#teamList").after($.templates("#teamList").render(data));
                }
            }
        })
    },

    /** 赛事聚焦 **/
    getNewsList: function (param) {
        $.ajax({
            url: ROOT_API + 'common/newsList',
            type: 'GET',
            data: param,
            dataType: 'JSON',
            success: function (response) {
                var code = response.code;
                var data = response.data;
                var s = param.type;
                if(code == 0){
                    if(s == 1){
                        $("#newList").after($.templates("#newList").render(data));
                    }else {
                        $("#reviewList").after($.templates("#reviewList").render(data));
                    }
                }
            }
        })
    },

    //文章详情
    getNewsInfo: function (id) {
        $.ajax({
            url: ROOT_API + 'common/newsInfo',
            type: 'GET',
            data: id,
            dataType: 'JSON',
            success: function (response) {
                var code = response.code;
                var data = response.data;
                if(code == 0){
                    $("#newsContent").after($.templates("#newsContent").render(data));
                }
            }
        })
    },

    /** 赛事阶段 **/
    //获取赛事阶段列表
    getSubMatchList: function () {
        $.ajax({
            url: ROOT_API + 'common/subMatchList',
            type: 'GET',
            data: '',
            dataType: 'JSON',
            success: function (response) {
                var code = response.code;
                var data = response.data;
                if(code == 0){

                    for(var i = 0; i < 4; i++){
                        var date = data[i].formatTime;
                        var light = data[i].isHightLight;
                        var id = data[i].id;

                        $('#period .time-slot').eq(i).text(date);
                        $('#period .star').eq(i).data('subMatchId', id);
                        if(!light){
                            $('#period .star').eq(i).addClass('grays').data('isHightLight', false);
                        }else {
                            $('#period .star').eq(i).data('isHightLight', true);
                        }
                    }
                }
            }
        })
    },
    //校验答题权限
    validationPermission: function (param) {
        $.ajax({
            url: ROOT_API + 'exam/check',
            type: 'GET',
            data: param,
            dataType: 'JSON',
            success: function (response) {
                var code = response.code;
                var msg = response.msg;
                if(code == 0){
                    //页面跳转到答题界面
                    //window.location.href = 'm_exma_detail.html'
                    PKTZ_API.getPageRoute('answer')
                }else if(code == 10) {
                    //跳转答题记录页面
                    PKTZ_API.getPageRoute('exam_record')
                }else {
                    weui.dialog({
                        content: msg,
                        className: 'custom-classname',
                        buttons: [{
                            label: '确定',
                            type: 'primary',
                            onClick: function () {}
                        }]
                    });
                }
            },
            error: function (response) {
                var msg = response.msg;
                weui.dialog({
                    content: msg,
                    className: 'custom-classname',
                    buttons: [{
                        label: '确定',
                        type: 'primary',
                        onClick: function () {}
                    }]
                });
            }
        })
    },
    //通过赛事阶段ID获取试题
    getPlayExam: function (id) {
        $.ajax({
            url: ROOT_API + 'exam/playExam',
            type: 'GET',
            data: id,
            dataType: 'JSON',
            async: false,
            success: function (response) {
                var code = response.code;
                var data = response.data;
                if(code == 0){
                    var singleList = data.singleQuestionList;
                    var multipleList = data.otherQuestionList;
                    //赛事阶段
                    var subMatchName = data.subMatchName;
                    //难度系数

                    //总共答题时间
                    var limitTime = data.limitTime;
                    //已用答题时间
                    var answerTime = data.answerTime;
                    //剩余答题时间
                    var remainingTime = limitTime - answerTime;
                    //试题ID
                    var examId = data.id;
                    //题目ID
                    var questionId = data.questionId;

                    $('.question-content').data({
                        'examId': examId,
                        'questionId': questionId,
                        'remainingTime': remainingTime,
                        'answerTime': answerTime,
                        'limitTime': limitTime
                    });
                    $('.subMatchName').text(subMatchName);
                    $('.document-title').text(subMatchName);

                    //遍历单选题列表，渲染进页面单选题容器
                    singleList.forEach(function (value, index) {
                        var options = value.options.split('|');
                        var li_modle = '<li>' +
                                            '<p class="topic">'+ (index + 1) + '、' + value.title +'</p>' +
                                            '<ul class="options-ul">' +
                                            '</ul>' +
                                        '</li>';
                        $('.single-choice').find('.single-ul').append(li_modle);
                        options.forEach(function (value2) {
                            var tip = value2.substr(0, 1);
                            var option = '<li>' +
                                            '<input type="radio" name="single'+ index +'" value="'+ tip +'">' +
                                            '<label>'+ value2 +'</label>' +
                                         '</li>';
                            $('.single-choice .single-ul>li').eq(index).find('.options-ul').append(option);
                        })
                    });

                    //遍历多选题列表，渲染进页面多选题容器
                    multipleList.forEach(function (value, index) {
                        var options = value.options.split('|');
                        var li_modle = '<li>' +
                                            '<p class="topic">'+ (singleList.length + index + 1) + '、' + value.title +'</p>' +
                                            '<ul class="options-ul">' +
                                            '</ul>' +
                                        '</li>';
                        $('.multiple-choices').find('.multiple-ul').append(li_modle);
                        options.forEach(function (value2) {
                            var tip = value2.substr(0, 1);
                            var option = '<li>' +
                                            '<input type="checkbox" name="multiple'+ index +'" value="'+ tip +'">' +
                                            '<label>'+ value2 +'</label>' +
                                         '</li>';
                            $('.multiple-choices .multiple-ul>li').eq(index).find('.options-ul').append(option);
                        })
                    });

                    var answerList = data.submitQuestionAnswer.split('|');
                    for(var i = 0,len = answerList.length;i < len; i++){
                        $('.question-content').find('.options-ul').each(function (t) {
                            $(this).data('key', answerList[t]);
                        })
                    }
                }
            }
        })
    },

    //每做完一题，更新答题
    updateExam: function (param) {
        $.ajax({
            url: ROOT_API + 'exam/updateExam',
            type: 'POST',
            data: param,
            dataType: 'JSON',
            contentType: "application/x-www-form-urlencoded",
            success: function (response) {

            }
        })
    },

    //提交试题
    submitExam: function (param,callback) {
        $.ajax({
            url: ROOT_API + 'exam/submitExam',
            type: 'POST',
            data: param,
            dataType: 'JSON',
            contentType: "application/x-www-form-urlencoded",
            success: function (response) {
                var code = response.code;
                var data = response.data;
                var msg = response.msg;
                if(code == 0){
                    weui.toast('提交完成！', {
                        duration: 1500,
                        className: 'custom-exam',
                        callback: function(){
                            var score = data.score;
                            var total = data.answerTime;
                            var m = total/60 < 10 ? ('0' + parseInt(total/60)) : parseInt(total/60);
                            var s = (total%60) < 10 ? ('0' + total%60) : total%60;
                            $('.answer-end .score').find('.number').text(score + '分');
                            $('.answer-end .used-time').find('.time').text(m + '分' + s + '秒');
                            callback()
                        }
                    });
                }else {
                    weui.dialog({
                        content: msg,
                        buttons: [
                            {
                                label: "我知道了",
                                type: "primary",
                                onClick: function () {
                                    PKTZ_API.getPageRoute('challenger');
                                }
                            }
                        ]
                    });
                }
            }
        })
    },
    //答题历史记录
    examHistory: function (param, callback) {
        $.ajax({
            url: ROOT_API + 'exam/history',
            type: 'GET',
            data: param,
            dataType: 'JSON',
            success: function (response) {
                var code = response.code;
                var data = response.data.exam;
                var subMatch = response.data.subMatch;
                if(code == 0){
                    var total = data.answerTime;
                    var s = (total%60) < 10 ? ('0' + total%60) : total%60;
                    var h = total/3600 < 10 ? ('0' + parseInt(total/3600)) : parseInt(total/3600);
                    var m = (total-h*3600)/60 < 10 ? ('0' + parseInt((total-h*3600)/60)) : parseInt((total-h*3600)/60);
                    $('.pic').find('img').attr('src', subMatch.img);
                    $('.exam-msg').find('.degree').text(data.difficultLevle + '级');
                    $('.exam-msg').find('.sub-match').text(subMatch.subMatchName);
                    $('.exam-msg').find('.time-used').text(m + '分' + s + '秒');
                    $('.exam-msg').find('.score').text(data.score + '分');
                    //单选题列表
                    var singleList = data.singleQuestionList;
                    singleList.forEach(function (value, index) {
                        var options = value.options.split('|');
                        var li_modle = '<li>' +
                                        '<p class="subject-title">' + (index + 1) + '、' + value.title + '</p>' +
                                        '<ul class="options" rightKey="'+ value.answer +'">' +
                                        '</ul>' +
                                        '<div class="right-key">' +
                                        '答案：<span class="key">'+ value.answer +'</span>' +
                                        '</div>' +
                                        '</li>';
                        $('.single-choice').find('.subject-ul').append(li_modle);
                        options.forEach(function (value2) {
                            var tip = value2.substr(0, 1);
                            var option = '<li>' +
                                         '<div class="item">' +
                                         '<span class="icon"></span>' +
                                         '<span class="text" keyValue="'+ tip +'">'+ value2 +'</span>' +
                                         '</div>' +
                                         '</li>';
                            $('.single-choice .subject-ul>li').eq(index).find('.options').append(option);
                        })
                    });

                    //多选题列表
                    var multipleList = data.otherQuestionList;
                    multipleList.forEach(function (value, index) {
                        var options = value.options.split('|');
                        var li_modle = '<li>' +
                                        '<p class="subject-title">' + (index + singleList.length + 1) + '、' + value.title + '</p>' +
                                        '<ul class="options" rightKey="'+ value.answer +'">' +
                                        '</ul>' +
                                        '<div class="right-key">' +
                                        '答案：<span class="key">'+ value.answer +'</span>' +
                                        '</div>' +
                                        '</li>';
                        $('.multiple-choice').find('.subject-ul').append(li_modle);
                        options.forEach(function (value2) {
                            var tip = value2.substr(0, 1);
                            var option = '<li>' +
                                         '<div class="item">' +
                                         '<span class="icon"></span>' +
                                         '<span class="text" keyValue="'+ tip +'">'+ value2 +'</span>' +
                                         '</div>' +
                                         '</li>';
                            $('.multiple-choice .subject-ul>li').eq(index).find('.options').append(option);
                        })
                    });
                    var answerList = data.submitQuestionAnswer.split('|');
                    $('.content .subject-ul').find('.options').each(function (t) {
                        $(this).attr('userKey', answerList[t]);
                    });

                    callback();
                }
            }
        })
    },

    //根据赛区和控制ID获取赛区队伍信息
    getAreaBettingTeams: function (param) {
        $.ajax({
            url: ROOT_API + 'betting/matchAreaBettingTeams',
            type: 'GET',
            data: param,
            dataType: 'JSON',
            success: function (response) {
                var code = response.code;
                var data = response.data;
                data.betStatus = PKTZ_API.getCookie('betStatus');
                if(code == 0){
                    $('.guess .team-list').find('li').remove();
                    $("#teamListBefore").after($.templates("#teamListBefore").render(data));
                }
            }
        })
    },

    //获取荣耀战报
    getGloryReport: function (id, callback) {
        $.ajax({
            url: ROOT_API + 'common/gloryReport',
            type: 'GET',
            data: id,
            dataType: 'JSON',
            success: function (response) {
                var code = response.code;
                var data = response.data;
                if(code == 0){
                    var img = data.matchImg;
                    $('.zhanbao-topBar').find('img').attr('src', img);
                    $('#target').find('li').remove();
                    $("#gloryReport").before($.templates("#gloryReport").render(data.totalRank));
                    callback('target');
                }
            }
        })
    },

};